package br.scvl.dominio.util;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import br.usjt.component.login.facade.LoginBeanRemote;

public class RemoteEJBClient {

	// public static void main(String[] args) throws Exception {
	// testRemoteEJB();
	//
	// }
	//
	// private static void testRemoteEJB() throws NamingException {
	//
	// final LoginBeanRemote ejb = lookupRemoteEJB();
	// String s = ejb.sayHello();
	// System.out.println(s);
	// }

	public static LoginBeanRemote lookupRemoteEJB() throws NamingException {

		final Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
		jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

		final Context context = new InitialContext(jndiProperties);

		final String appName = "login-ear";
		final String moduleName = "login-ejb";
		final String distinctName = "";
		final String beanName = "LoginBean";

		final String viewClassName = LoginBeanRemote.class.getName();
		System.out.println("Looking EJB via JNDI ");
		System.out.println("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);

		return (LoginBeanRemote) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);

	}

	public static Object lookupRemoteEJB(Class<?> clazz) throws NamingException {

		final Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
		jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

		final Context context = new InitialContext(jndiProperties);

		final String appName = "login-ear";
		final String moduleName = "login-ejb";
		final String distinctName = "";
		final String beanName = clazz.getSimpleName().substring(0, clazz.getSimpleName().lastIndexOf("R"));

		final String viewClassName = clazz.getName();
		System.out.println("Looking EJB via JNDI ");
		System.out.println("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);

		return context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);

	}
	// public static LoginBeanRemote lookupRemoteEJB() throws NamingException {
	//
	// Properties pr = new Properties();
	//
	// pr.put("endpoint.name", "client-endpoint");
	// //
	// pr.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED",
	// // "false");
	// pr.put("remote.connections", "default");
	// pr.put("remote.connection.default.host", "localhost");
	// pr.put("remote.connection.default.port", "4447");
	// //
	// pr.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS",
	// // "false");
	// pr.put("remote.connection.default.username", "user1234");
	// pr.put("remote.connection.default.password", "password1234");
	//
	// EJBClientConfiguration cc = new
	// PropertiesBasedEJBClientConfiguration(pr);
	//
	// final ContextSelector<EJBClientContext> ejbClientContextSelector = new
	// ConfigBasedEJBClientContextSelector(cc);
	//
	// // Now let's setup the EJBClientContext to use this selector
	//
	// final ContextSelector<EJBClientContext> previousSelector =
	// EJBClientContext.setSelector(ejbClientContextSelector);
	//
	// StatelessEJBLocator<LoginBeanRemote> locator = new
	// StatelessEJBLocator(LoginBeanRemote.class, "login-ear", "login-ejb",
	// "LoginBean", "");
	//
	// LoginBeanRemote ejb =
	// org.jboss.ejb.client.EJBClient.createProxy(locator);
	//
	// return ejb;
	//
	// }
}
